/*
 * ActionShowWidget.cpp
 *
 *  Created on: 2015-9-23
 *      Author: 10095427
 */

#include "ActionShowWidget.h"

#define   WIDTH   16
#define   HEIGHT   16
#define   MOVE_TIME_GAP  0.1

ActionShowWidget::ActionShowWidget()
:m_type(0)
{

}

ActionShowWidget::~ActionShowWidget() {
}

ActionShowWidget* ActionShowWidget::create(int type)
{
	ActionShowWidget *pRet = new(std::nothrow) ActionShowWidget();
    if (pRet && pRet->init(type)){
        pRet->autorelease();
        return pRet;
    }
    else
    {
        delete pRet;
        pRet = NULL;
        return NULL;
    }
}

bool ActionShowWidget::init(int type)
{
    if (!Layer::init()) {
        return false;
    }
    m_type = type;
    actionInit();
    return true;
}

void ActionShowWidget::actionInit()
{
	Texture2D *tempTexture = Director::getInstance()->getTextureCache()->addImage(getFileByType());
	SpriteFrame *tempFrame = SpriteFrame::createWithTexture(tempTexture, Rect(0,0,WIDTH,HEIGHT));
	auto m_rolerSprite = Sprite::createWithSpriteFrame(tempFrame);
	m_rolerSprite->setAnchorPoint(Vec2::ZERO);
	m_rolerSprite->setPosition(Vec2::ZERO);

	Vector<cocos2d::SpriteFrame *> tempFrames;
	for (int i=0; i<4; i++){
	    for (int j=0; j<4; j++)
	    {
	        auto frame = SpriteFrame::createWithTexture(tempTexture, Rect(j*WIDTH,i*HEIGHT,WIDTH,HEIGHT));
	        tempFrames.pushBack(frame);
	    }
    }
    auto tempAnimation = Animation::createWithSpriteFrames(tempFrames, MOVE_TIME_GAP);
    Animate* tempAction = Animate::create(tempAnimation);
	m_rolerSprite->runAction(RepeatForever::create(tempAction));
	addChild(m_rolerSprite);
	m_rolerSprite->runAction(Sequence::create(tempAction,
			CallFunc::create(CC_CALLBACK_0(Sprite::removeFromParent, m_rolerSprite)),
			CallFuncN::create(CC_CALLBACK_1(ActionShowWidget::actionEndCallback, this)),
		        		                           NULL));


}

void ActionShowWidget::actionEndCallback(Node *node)
{
	m_callback(this);
}

const char* ActionShowWidget::getFileByType()
{
	switch(m_type){
	    case ActionShowWidgetTypeTransferBlack:
	    	return "TransferBlack.png";
	}
	return "";
}

void ActionShowWidget::setCallback(const actionShowWidgetCallback& callback)
{
	m_callback = callback;
}
